package com.dries.portal.module.coupon.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;
import java.util.stream.Stream;

/**
 * @Description: @JsonFormat(shape = JsonFormat.Shape.OBJECT)
 * @Author ZSY
 * @createTime 2020/7/28 15:16
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum CouponStatus {

    UNCLAIMED(0, "未领取"),
    USABLE(1, "可用的"),
    USED(2, "已使用"),
    EXPIRED(3, "过期的（未被使用的）");

    @EnumValue
    private Integer code;
    private String desc;

    @JsonValue
    public String getDesc() {
        return desc;
    }

    /** 根据code获取到CouponStatus */
    public static CouponStatus of(Integer code) {
        Objects.requireNonNull(code);

        return Stream.of(values()).filter(e -> e.getCode().equals(code))
                .findAny()
                .orElseThrow(() -> new IllegalArgumentException(code + "not exists!"));
    }
}
